package view;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import core.EventBus;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class PlayerSettings extends JFrame {
	private JLabel nameLabel;
	private JTextField nameTextField;
	private JComboBox colorComboBox;
	private JLabel colorLabel;
	private AbstractAction cancelAction;
	private JButton cancelButton;
	private AbstractAction okAction;
	private JButton okButton;
	private EventBus m;

	public PlayerSettings(EventBus m) {
		this.m = m;
		initGUI();
		setVisible(false);
		setLocationRelativeTo(null);
		
	}

	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				this.setPreferredSize(new java.awt.Dimension(400, 400));
				getContentPane().setBackground(new java.awt.Color(255,255,255));
				this.setTitle("Player Settings");
				this.setAlwaysOnTop(true);
				this.setResizable(false);
				{
					nameLabel = new JLabel();
					getContentPane().add(nameLabel);
					nameLabel.setText("Name: ");
					nameLabel.setBounds(12, 12, 86, 18);
				}
				{
					nameTextField = new JTextField();
					getContentPane().add(nameTextField);
					nameTextField.setText("Enter you name here");
					nameTextField.setBounds(64, 10, 129, 23);
					nameTextField.setToolTipText("Enter the name you would like to be presented as you play your game");
				}
				{
					okButton = new JButton();
					getContentPane().add(okButton);				
					getContentPane().add(getCancleButton());
					getContentPane().add(getColorLabel());
					getContentPane().add(getColorComboBox());
					okButton.setBounds(307, 327, 57, 34);
					okButton.setAction(getOkAction());
					okButton.setText("OK");
				}
				this.setSize(400, 400);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private AbstractAction getOkAction() {
		if(okAction == null) {
			okAction = new AbstractAction("OK", null) {
				public void actionPerformed(ActionEvent evt) {
					m.handleEvent(new ActionEvent(getPlayerName(), 0, "PLAYERNAME"));
					setVisible(false);
				}
			};
		}
		return okAction;
	}
	/**
	 * 
	 * @return returns the specified name entered by the user. Returns Default if no name has been set.
	 */
	public String getPlayerName() {
		if(!nameTextField.getText().equalsIgnoreCase("Enter you name here"))
			return nameTextField.getText();
		else
			return "Default";
	}
	
	private JButton getCancleButton() {
		if(cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.setBounds(218, 327, 84, 34);
			cancelButton.setAction(getCancleAction());
		}
		return cancelButton;
	}
	
	private AbstractAction getCancleAction() {
		if(cancelAction == null) {
			cancelAction = new AbstractAction("Cancel", null) {
				public void actionPerformed(ActionEvent evt) {
					setVisible(false);
				}
			};
		}
		return cancelAction;
	}
	
	private JLabel getColorLabel() {
		if(colorLabel == null) {
			colorLabel = new JLabel();
			colorLabel.setText("Color: ");
			colorLabel.setBounds(12, 42, 41, 16);
		}
		return colorLabel;
	}
	
	private JComboBox getColorComboBox() {
		if(colorComboBox == null) {
			ComboBoxModel colorComboBoxModel = 
				new DefaultComboBoxModel(
						new String[] { "Blue", "Green", "Yellow", "Cyan", "Grey", "Brown", "Pink" });
			colorComboBox = new JComboBox();
			colorComboBox.setModel(colorComboBoxModel);
			colorComboBox.setBounds(65, 39, 59, 23);
			colorComboBox.setToolTipText("Select the color you would like");
			
		}
		return colorComboBox;
	}
}
